package com.ace.log.core.file.name

import java.text.SimpleDateFormat
import java.util.*

/**
 * 基于 yyyy-MM-dd 的日期格式生成文件
 */
class DateFileNameGenerator : IFileNameGenerator {

    private val mLocalDateFormat: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue() = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        }

    override fun isFileNameChangeable() = true

    override fun generateFileName(
        logLevel: Int,
        tag: String,
        timestamp: Long,
        lastFileName: String
    ): String {

        val sdf = mLocalDateFormat.get() ?: SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)

        return sdf.let {
            it.timeZone = TimeZone.getDefault()
            it.format(Date(timestamp))
        }
    }
}